package com.vincent.kdp.ui.commodity.presenter;

import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

import com.vincent.baseapp.base.BasePresenter;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.bean.entity.LikeCommodity;
import com.vincent.kdp.ui.bean.response.CommodityDetailOutput;
import com.vincent.kdp.ui.bean.response.LikeCommodityOutput;
import com.vincent.kdp.ui.commodity.contract.CommodityDetailContract;
import com.vincent.kdp.ui.commodity.repository.CommodityDetailRepository;
import com.vincent.kdp.util.BaseParam;

import java.util.ArrayList;

public class CommodityDetailPresenter extends BasePresenter<CommodityDetailContract.View> implements
        CommodityDetailContract.Presenter {

    private CommodityDetailRepository mRepository;

    public CommodityDetailPresenter() {
        mRepository = new CommodityDetailRepository();
    }

    @Override
    public void initView(Intent intent) {
        // TODO: 2016/10/25 这里没有传递员工的departId也可以请求接口，要和后台沟通一下
        String barCode=intent.getStringExtra("BarCode");
        String deviceId=intent.getStringExtra("deviceId");
//        TelephonyManager telephonyManager = (TelephonyManager) getSystemService( Context.TELEPHONY_SERVICE );
//        String photoCode =telephonyManager.getDeviceId();

        reqCommodityDetail(barCode, BaseParam.getDeviceId());
        reqLikeCommodityList();
    }

    /**
     * 获取商品详细信息
     * @param barCode  商品编码
     * @param number 设备号
     */
    private void reqCommodityDetail(String barCode, String number) {
        mView.showLoadDialog();
        mRepository.reqCommodityDetail(barCode, number, new RequestCallBack<CommodityDetailOutput>() {
            @Override
            public void onSuccess(CommodityDetailOutput object) {
                if (mView != null) {
                    mView.dismissLoadDialog();
                    mView.refreshCommodityDetail(object.getData());
                }
            }

            @Override
            public void onFalse(String msg) {
                if (mView != null) {
                    mView.dismissLoadDialog();
                    mView.showToast(msg);
                }
            }
        });
    }

    /**
     * 获取猜你喜欢的商品列表
     */
    private void reqLikeCommodityList() {
        mView.showLoadDialog();
        mRepository.reqLikeCommodityList(new RequestCallBack<LikeCommodityOutput>() {
            @Override
            public void onSuccess(LikeCommodityOutput object) {
                if (mView != null) {
                    mView.dismissLoadDialog();
                    mView.initLikeRecycle(object.getData() == null ? new ArrayList<LikeCommodity>() : object.getData());
                }
            }

            @Override
            public void onFalse(String msg) {
                if (mView != null) {
                    mView.dismissLoadDialog();
                    mView.showToast(msg);
                }
            }
        });
    }
}
